package examples.mail;

import java.sql.SQLException;
import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.ColumnListHandler;

public class MxCheck {
	int max= 100;
	
	QueryRunner qr = null;
	BlockingQueue<String> bq = new LinkedBlockingQueue<String>();
	String sql = " update nomx set stat = ?  where att = ?";

	public MxCheck() throws Exception {
		qr = Db.getQueryRunner();
		List<Object> ls = qr.query("select att from nomx where stat is  null  ", new ColumnListHandler("att"));
		for (Object o : ls) {
			String s = (String) o;
			bq.add(s);       
		}
		for(int i =0 ;i<max ;i++){
			new WorkThread().start();
		}

	}

	class WorkThread extends Thread {
		public void run() {
			String att = null;
			while ((att = bq.poll()) != null) {
				try {
					List<String> mxs = DnsResolver.getMXRecordsForHost(att);
					if (mxs != null && mxs.size() > 0) {

						qr.update(sql, "1", att);

					} else {
						qr.update(sql, "0", att);
					}
				} catch (SQLException e) {
					e.printStackTrace();
				}

			}
		}
	}

	public static void main(String[] args) throws Exception {
		new MxCheck();
	}

}
